package database;

import java.sql.*;
import java.util.Scanner;

public class TextDBUtil {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean running = true;

        while (running) {
            System.out.println("请选择操作：");
            System.out.println("1. 添加用户");
            System.out.println("2. 更新用户邮箱");
            System.out.println("3. 删除用户");
            System.out.println("4. 查询用户");
            System.out.println("5. 退出");

            String choice = input.next();

            switch (choice) {
                case "1":
                    addUser(input);
                    break;
                case "2":
                    updateUserEmail(input);
                    break;
                case "3":
                    deleteUser(input);
                    break;
                case "4":
                    queryUser(input);
                    break;
                case "5":
                    running = false;
                    break;
                default:
                    System.out.println("无效的选项，请重新选择！");
            }
        }

        System.out.println("程序已退出。");
    }

    private static void addUser(Scanner input) {
        System.out.println("请输入用户名：");
        String username = input.next();
        System.out.println("请输入邮箱：");
        String email = input.next();

        int result = DBUtil.executeUpdate("INSERT INTO users (username, email) VALUES ('" + username + "', '" + email + "')");
        if (result > 0) {
            System.out.println("用户添加成功。");
        } else {
            System.out.println("用户添加失败。");
        }
    }

    private static void updateUserEmail(Scanner input) {
        System.out.println("请输入要更新邮箱的用户名：");
        String username = input.next();
        System.out.println("请输入新的邮箱：");
        String newEmail = input.next();

        int result = DBUtil.executeUpdate("UPDATE users SET email = '" + newEmail + "' WHERE username = '" + username + "'");
        if (result > 0) {
            System.out.println("邮箱更新成功。");
        } else {
            System.out.println("邮箱更新失败。");
        }
    }

    private static void deleteUser(Scanner input) {
        System.out.println("请输入要删除的用户名：");
        String username = input.next();

        int result = DBUtil.executeUpdate("DELETE FROM users WHERE username = '" + username + "'");
        if (result > 0) {
            System.out.println("用户删除成功。");
        } else {
            System.out.println("用户删除失败。");
        }
    }

    private static void queryUser(Scanner input) {
        System.out.println("请输入要查询的用户名：");
        String username = input.next();

        try (Connection connection = DBUtil.getConn();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery("SELECT * FROM users WHERE username = '" + username + "'")) {

            if (resultSet.next()) {
                String foundUsername = resultSet.getString("username");
                String email = resultSet.getString("email");
                System.out.println("用户名: " + foundUsername + ", 邮箱: " + email);
            } else {
                System.out.println("未找到用户名为 " + username + " 的用户。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
